package com.hd.controller.activiti.util.cmd;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.activiti.engine.impl.context.Context;
import org.activiti.engine.impl.interceptor.Command;
import org.activiti.engine.impl.interceptor.CommandContext;
import org.activiti.engine.impl.persistence.entity.ExecutionEntity;
import org.activiti.engine.impl.persistence.entity.ExecutionEntityManager;
import org.activiti.engine.impl.persistence.entity.TaskEntity;
import org.activiti.engine.impl.pvm.process.ActivityImpl;
/**
 * 多实例跳转 参考 Activiti权威指南
 * @author hzhh123
 * 2018年8月24日上午9:46:56
 * 第34~42查找执行实例对应的顶级流程实例，直到找到为止，第47~49查找顶级流程实例下的所有任务，
 * 然后第50~56循环遍历任务并将其删除，第57~58行获取流程实例
 *	下的执行实例，第61~62行遍历所有执行实例集合，第63~66行删除执行实例，
 *第81行删除当前流程实例对应的ACT_RU_IDENTITYLINK表中数据，以避免脏数据
 */
public class MultiInstanceJumpCmd implements Command<Void> {
	protected String executionId;//执行实例id
	protected String parentId;//流程实例id
	protected ActivityImpl desActivity;//目标节点
	protected Map<String, Object> paramvar;//变量
	protected ActivityImpl currentActivity;//当前的节点
	@Override
	public Void execute(CommandContext commandContext) {
		ExecutionEntityManager executionEntityManager = Context
				.getCommandContext().getExecutionEntityManager();
		ExecutionEntity executionEntity = executionEntityManager
				.findExecutionById(executionId);
		String id = null;
		if (executionEntity.getParent() != null) {
			executionEntity = executionEntity.getParent();
			if (executionEntity.getParent() != null) {
				executionEntity = executionEntity.getParent();
				id = executionEntity.getId();
			}
			id = executionEntity.getId();
		}
		executionEntity.setVariables(paramvar);
		executionEntity.setExecutions(null);
		executionEntity.setEventSource(this.currentActivity);
		executionEntity.setActivity(this.currentActivity);
		// 根据executionId 获取Task
		Iterator<TaskEntity> localIterator = Context.getCommandContext()
				.getTaskEntityManager().findTasksByProcessInstanceId(id).iterator();

		while (localIterator.hasNext()) {
			TaskEntity taskEntity = (TaskEntity) localIterator.next();
			System.err.println("==================" + taskEntity.getId());
			// 触发任务监听
			taskEntity.fireEvent("complete");
			// 删除任务的原因
			Context.getCommandContext().getTaskEntityManager()
					.deleteTask(taskEntity, "completed", false);
		}
		List<ExecutionEntity> list = executionEntityManager
				.findChildExecutionsByParentExecutionId(parentId);
		for (ExecutionEntity executionEntity2 : list) {
			ExecutionEntity findExecutionById = executionEntityManager.findExecutionById(executionEntity2.getId());

			List<ExecutionEntity> parent = executionEntityManager
					.findChildExecutionsByParentExecutionId(executionEntity2
							.getId());
			for (ExecutionEntity executionEntity3 : parent) {
				executionEntity3.remove();
				System.err.println(executionEntity3.getId()
						+ "----------------->>>>>>>>>>");
				Context.getCommandContext().getHistoryManager()
						.recordActivityEnd(executionEntity3);
			}
			executionEntity2.remove();
	    	  Context.getCommandContext().getHistoryManager().recordActivityEnd(executionEntity2);
	    	  System.err.println(findExecutionById+"----------------->>>>>>>>>>");

		}
	      commandContext
	        .getIdentityLinkEntityManager().deleteIdentityLinksByProcInstance(id);
		executionEntity.executeActivity(this.desActivity);
		//

		return null;
	}
	public MultiInstanceJumpCmd(String executionId, String parentId, ActivityImpl desActivity,
			Map<String, Object> paramvar, ActivityImpl currentActivity) {
		super();
		this.executionId = executionId;
		this.parentId = parentId;
		this.desActivity = desActivity;
		this.paramvar = paramvar;
		this.currentActivity = currentActivity;
	}
	
}
